home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / c / library / dos / diverses / leda / incl / ab_tree.h next >
Encoding:
C/C++ Source or Header  |  1991-11-15  |  6.3 KB  |  210 lines

  1. /*******************************************************************************
  2. +
  3. +  LEDA  2.1.1                                                 11-15-1991
  4. +
  5. +
  6. +  ab_tree.h
  7. +
  8. +
  9. +  Copyright (c) 1991  by  Max-Planck-Institut fuer Informatik
  10. +  Im Stadtwald, 6600 Saarbruecken, FRG     
  11. +  All rights reserved.
  12. *******************************************************************************/
  13.  
  14.  
  15.  
  16.  
  17.  
  18. #ifndef AB_TREE_H
  19. #define AB_TREE_H
  20.  
  21. //------------------------------------------------------------------------------
  22. //
  23. // (a,b)-trees 
  24. //
  25. // Evelyn Haak, Juergen Dedorath, and Dirk Basenach   (1989)
  26. //
  27. // Implementation follows
  28. // Kurt Mehlhorn : "Data Structures and Algorithms 1", section III, 5.2 - 5.3
  29. //
  30. // Modifications:
  31. // -------------
  32. //
  33. // - member-function insert_at_node added  ( Michael Wenzel , Nov. 89)
  34. //
  35. // - virtual compare function ( Michael Wenzel , Nov. 89)
  36. //
  37. // - delete : overwrite copies of inner nodes by
  38. //            following links between different
  39. //            instances of the same key    ( Michael Wenzel , Jan. 90)
  40. //
  41. // - virtual clear functions  ( S. Naeher, Mai 90)
  42. //
  43. //------------------------------------------------------------------------------
  44.  
  45. #include    <LEDA/basic.h>
  46.  
  47. //------------------------------------------------------------------------------
  48. //  some declarations
  49. //------------------------------------------------------------------------------
  50.  
  51.  
  52. class ab_tree;
  53. class ab_tree_node;
  54.  
  55. typedef ab_tree_node* abnode;
  56.  
  57. typedef ab_tree_node* ab_tree_item;
  58.  
  59.  
  60. /*------------------------------------------------------------*/
  61. /*  class ab_tree_node                                        */
  62. /*------------------------------------------------------------*/
  63.  
  64. class ab_tree_node {
  65.   friend class ab_tree;
  66.  
  67.   friend void concat(ab_tree& s1,ab_tree& s2,ab_tree_node* current,ent cur_key);
  68.   friend void pr_ab_tree(ab_tree_node* localroot,int blancs);
  69.   friend void del_ab_tree(ab_tree_node* localroot);
  70.  
  71.  
  72.   int p;           /* number of sons,a<=p<=b,0 iff leave*/
  73.   int height;      /* height of node*/
  74.   int index;       /* v is index'th son of his father*/
  75.   ab_tree_node* father;   
  76.   ent* k;          /* array[1..b] --------------------------  */
  77.                          //-----------------------------------
  78.                          /*to every node v of T we assign p(v)-1 */
  79.                          /*elements k[1](v),...,k[p(v)-1](v) of U*/
  80.                          /* such that for all i (1<i<p(v)-1):*/
  81.                          /* k[i](v) < k[i+1](v) and for all leaves*/
  82.                          /* w in the i-th subtree of v we have*/
  83.                          /* k[i-1] < key[w] <= k[i](v) */
  84.                          /*------------------------------------*/
  85.                          /* if v is a leave ==> k[1]=key[v]*/
  86.  
  87.                          
  88.   ab_tree_node** son;    /* array [1..b+1] of pointer to sons*/
  89.   ab_tree_node** same;   /* m.w. : links between same keys */
  90.                          /* array [1..b]                   */
  91.   ent inf;
  92.  
  93. /* size = 8 words  */
  94.  
  95. public:
  96.  
  97.   ab_tree_node(int p,int height,int index,ab_tree_node* father,int b);
  98.  
  99.  ~ab_tree_node();
  100.  
  101.    OPERATOR_NEW(8)
  102.    OPERATOR_DEL(8)
  103.  
  104.  }; 
  105.   
  106. /*-----------------------------------------------------------------*/
  107. class ab_tree   
  108.    {
  109.  
  110.     friend class ab_tree_node;
  111.  
  112.     friend void concat(ab_tree&,ab_tree&,ab_tree_node*,ent); 
  113.  
  114.  
  115.     friend void pr_ab_tree(ab_tree_node* localroot,int blancs);
  116.     friend void del_ab_tree(ab_tree_node* localroot);
  117.  
  118.     int a;
  119.     int b;
  120.  
  121.     int height;             /* height of tree   */
  122.     int count;              /* number of leaves */
  123.  
  124.     ab_tree_node* root;
  125.     ab_tree_node* minimum;  
  126.     ab_tree_node* maximum;
  127.  
  128.     ab_tree_node* expand(ab_tree_node* v,int i);
  129.  
  130.     void split_node(ab_tree_node* v);
  131.     void share(ab_tree_node* v,ab_tree_node* y,int direct);
  132.     void fuse (ab_tree_node* v,ab_tree_node* w);
  133.     void del_tree(ab_tree_node* localroot);
  134.     void exchange_leaves(ab_tree_node*, ab_tree_node*);
  135.     void pr_ab_tree(ab_tree_node*,int) const;
  136.  
  137.  
  138.  
  139.     ab_tree_node* copy_ab_tree(ab_tree_node*,abnode&,int) const;
  140.  
  141.     virtual int cmp(ent& x,ent& y) const { return int(x)-int(y); }
  142.  
  143.     virtual void clear_key(ent& x) const { Clear(x); }
  144.     virtual void clear_inf(ent& x) const { Clear(x); }
  145.  
  146.     virtual void copy_key(ent& x)  const { Copy(x);  }
  147.     virtual void copy_inf(ent& x)  const { Copy(x);  }
  148.  
  149.     virtual void print_key(ent& x) const { Print(x); }
  150.     virtual void print_inf(ent& x) const { Print(x); }
  151.  
  152. public:
  153.  
  154.  
  155.  
  156.     void clear();
  157.  
  158.     ent  inf(ab_tree_node* p)  const { return p->inf; }
  159.     ent  key(ab_tree_node* p)  const { return p->k[1]; }
  160.  
  161.     ab_tree_node* insert(ent, ent);
  162.     ab_tree_node* insert_at_node(ab_tree_node*, ent, ent);
  163.     ab_tree_node* locate(ent) const;
  164.     ab_tree_node* locate_pred(ent) const;
  165.     ab_tree_node* lookup(ent) const;
  166.  
  167.     void del(ent);
  168.     void del_node(ab_tree_node*);
  169.     void change_inf(ab_tree_node*, ent);
  170.     void flip(ab_tree_node*, ab_tree_node*);
  171.     void reverse(ab_tree_node*, ab_tree_node*);
  172.  
  173.     ab_tree& conc(ab_tree&);
  174.     void split_at_item(ab_tree_node* w,ab_tree& L,ab_tree& R);
  175.  
  176.     void del_item(ab_tree_node* p) { del_node(p); }
  177.     void del_min()                 { if (minimum) del_item(minimum); }
  178.     void decrease_key(ab_tree_node* p, ent k) { ent i = p->inf;
  179.                                                 copy_key(i);
  180.                                                 del_item(p);
  181.                                                 insert(k,i);
  182.                                                 clear_key(i);
  183.                                                }
  184.  
  185.  
  186.     bool member(ent k)  const { return (lookup(k))? true: false ; }
  187.  
  188.     ab_tree_node* min()                      const { return minimum; }
  189.     ab_tree_node* find_min()                 const { return minimum; }
  190.     ab_tree_node* max()                      const { return maximum; }
  191.     ab_tree_node* first_item()               const { return minimum; }
  192.     ab_tree_node* next_item(ab_tree_node* p) const { return p->son[2]; }
  193.     ab_tree_node* succ(ab_tree_node* p)      const { return p->son[2]; }
  194.     ab_tree_node* pred(ab_tree_node* p)      const { return p->son[1]; }
  195.  
  196.     int  size()  const { return count;       }
  197.     bool empty() const { return (count==0) ? true : false;   }
  198.     void print() const { pr_ab_tree(root,1); }
  199.  
  200.     ab_tree(int a_=2,int b_=4); 
  201.     ab_tree(const ab_tree& T);
  202.  
  203.     ab_tree& operator=(const ab_tree&);
  204.  
  205.    ~ab_tree(){ clear(); }
  206.  };
  207.  
  208. #endif
  209.